package httpserver
{
	import ctimer.CTimer;
	import ctimer.Task;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;
	import flash.utils.Timer;

	public class HttpRequest extends EventDispatcher
	{
		
		public static const RETRY_COUNT:int = 2;               //请求超时后的重试测试
		public static const RETRY_GROWTH_INTERVAL_TIME:int = 1;  //重试时间间隔累加值
		public static const HTTP_REQUEST_ERROR:String = "http_reuqest_error";
		
		private var _request:URLRequest;
		private var _loader:URLLoader;
		private var _variables:URLVariables;
		private var _timer:CTimer;
		
		private var _retrySendRequestIndex:int = 0;
		
		private var _requestTask:Task;
/*		
		public function HttpRequest(url:String, requestJsonData:String, responsFunction:Function)
		{
			_timer = new CTimer();
			_timer.Run();
			
			_request = new URLRequest();
			_loader = new URLLoader();
			
			_loader.addEventListener(Event.COMPLETE, function(event:Event):void{
				loaderCompleteHandler(event, responsFunction);});
			_loader.addEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
			_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loaderHttpStatusHandler);
			
			_request.url = url;
			
			if (requestJsonData != null)
			{
				
				_variables = new URLVariables();
				_variables.device = requestJsonData;
				
				_request.data = _variables;
			}
			else
			{
				_request.data = null;
			}

		}
*/
		public function HttpRequest(url:String, requestJsonData:String, responsFunction:Function)
		{
			_timer = new CTimer();
			_timer.Run();
			
			_request = new URLRequest();
			_loader = new URLLoader();
			
			_loader.addEventListener(Event.COMPLETE, function(event:Event):void{
				loaderCompleteHandler(event, responsFunction);});
			_loader.addEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
			_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loaderHttpStatusHandler);
			
			_request.url = url;
			
			if (requestJsonData != null)
			{
				_request.method = URLRequestMethod.POST;
				_request.data = requestJsonData;
			}
			else
			{
				_request.data = null;
			}
			
		}
		
		public function sendRequest():void
		{
			loaderRequest(); //发送http数据请求		
		}
		
		private function loaderRequest():void
		{
			if (_loader != null){
				_loader.load(_request);
			}
		}
		
		private function loaderCompleteHandler(event:Event, responseFunction:Function):void
		{
			dispatchEvent(event);
			responseFunction(_loader.data);
		}
		
		private function loaderIOErrorHandler(event:Event):void{
			trace("IoErrorHandler: " + event);
		}
		
		private function loaderHttpStatusHandler(event:HTTPStatusEvent):void
		{
			trace("httpStatusHandler: " + event);
			trace("status: " + event.status);
			
			if (event.status == 200)
			{
				_retrySendRequestIndex = 0;
			}
			else
			{
				dispatchEvent(event);
				retrySendRequest(); //重新发送请求
			}
		}
		
		private function retrySendRequest():void
		{
			if (_retrySendRequestIndex > RETRY_COUNT)
			{
				//分发网络数据请求失败事件
				var event:Event = new Event(HTTP_REQUEST_ERROR);
				dispatchEvent(event);
				
				return;
			}
			
			var time:int = RETRY_GROWTH_INTERVAL_TIME + RETRY_GROWTH_INTERVAL_TIME*_retrySendRequestIndex;
			_requestTask = new Task(loaderRequest, time, false);
			_timer.AddTask(_requestTask);
			_retrySendRequestIndex++;
			
		}
		
	}
}